<?php
add_action('widgets_init', 'register_follow_widget');

function register_follow_widget()
{
	register_widget('follow');
}
class follow extends WP_Widget // widget class
{
	function follow() // widget setting | class name must be same function name
	{
		$widget_ops = array('classname' => 'Follow-w', 'description' =>
				'Display follow category, store form - Right side bar | Footer | Store detail');
		$control_ops = array(
			'width' => 250,
			'height' => 350,
			'id_base' => 'follow');
		$this->WP_Widget('follow', 'CouponX - Follow', $widget_ops, $control_ops);
	}
	function widget($args, $instance) // display widget
	{
		extract($args);
        		
		$title = apply_filters('widget_title', $instance['title']);
        $kw_after_subcribe_title = $instance['kw_after_subcribe_title'];
        $btn_text = $instance['btn_text'];
        $placeholder = $instance['pl_hd'];
        $display_position = $instance['display_option']; // Su dung khi co HTML CSS, Tuy vi tri hien thi ma hien thi HTML khac nhau
        
        if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) 
        {
            $current_url = explode('/', cpx_current_url());
            
            if($_SERVER["HTTP_X_FORWARDED_FOR"])
                $ip_address = $_SERVER["HTTP_X_FORWARDED_FOR"];
            else
                $ip_address = $_SERVER['REMOTE_ADDR'];
            $email = $_POST['email'];
            $time = current_time('mysql');
            
            $arr_values = array('user_email' => $email, 'time'=> $time, 'user_ip' => $ip_address);
            if(is_singular('store'))
            {
                global $post;
                $arr_values['follow_what'] = 'store';
                $arr_values['value'] = $post->ID;    
            }
            else if(is_page(''))
            {
                $arr_values['follow_what'] = 'category';
                $arr_values['value'] = 'unknow';
            }
            else
                $arr_values['follow_what'] = 'site';
            
            global $wpdb;
            $rows_affected = $wpdb->insert($wpdb->prefix.'follow', $arr_values);
        }
        //DISPLAY AT HOME SIDE BAR
        if($display_position == 'home-sidebar') include('inc/subscribe_home_side_bar.php');
        //DISPLAY AT FOOTER
        if($display_position == 'footer') include('inc/subscribe_footer.php'); 
        //DISPLAY AT STORE DETAIL PAGE
        if($display_position == 'store-detail') include('inc/subscribe_store_detail.php');
	}
    
	function update($new_instance, $old_instance) // update widget
	{
		$instance = $old_instance;		
        $instance['title'] = $new_instance['title'];
        $instance['display_option'] = $new_instance['display_option'];
        $instance['btn_text'] = $new_instance['btn_text'];
        $instance['pl_hd'] = $new_instance['pl_hd'];
        $instance['kw_after_subcribe_title'] = $new_instance['kw_after_subcribe_title'];
		return $instance;
	}
    
	function form($instance) // form for the widget options
	{
	   $arr_option = array('home-sidebar', 'footer' ,'category-listing', 'category-detail', 'store-listing', 'store-detail');
?>
        <div style="color: #333;">
    		<p>
    			<label for="<?php echo $this->get_field_id('title');?>"><?php echo 'Title'; ?></label>
    			<input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php	echo $this->get_field_name('title');?>" value="<?php echo $instance['title'];?>" style="width:100%;" />
    		</p>
            <p>
    			<label for="<?php echo $this->get_field_id('kw_after_subcribe_title');?>"><?php echo 'Keyword after title'; ?></label>
    			<input type="text" id="<?php echo $this->get_field_id('kw_after_subcribe_title'); ?>" name="<?php	echo $this->get_field_name('kw_after_subcribe_title');?>" value="<?php echo $instance['kw_after_subcribe_title'];?>" style="width:100%;" />
    		</p>
            <p>
                <label for="<?php echo $this->get_field_id( 'display_option' ); ?>"><?php _e('Position', ''); ?></label>
                <select name="<?php echo $this->get_field_name('display_option');?>" id="<?php echo $this-> get_field_id('display_option')?>" style="width: 60%;">
                <?php  
                        foreach ($arr_option as $v) {
                            if($instance['display_option'] == $v)
                                $selected = 'selected';
                            else
                                $selected = '';
                            echo "<option value='{$v}' {$selected}>$v</option>";
                        }
                ?>
                </select>    
            </p>
            <p>
                <label>Button text</label>
                <input type="text" placeholder="Follow, Subscribe, etc." id="<?php echo $this->get_field_id('btn_text'); ?>" name="<?php	echo $this->get_field_name('btn_text');?>" value="<?php echo $instance['btn_text'];?>" style="width:100%;" />
            </p>
            <p>
                <label>Input placeholder text</label>
                <input type="text" id="<?php echo $this->get_field_id('pl_hd'); ?>" name="<?php	echo $this->get_field_name('pl_hd');?>" value="<?php echo $instance['pl_hd'];?>" style="width:100%;" />
            </p>
        
        </div>
<?php
	}
}
?>
